<template>
  <div class="my-image">
    <div class="img_btn" @click="openDialog">
      <img :src="value||defaultImage" alt />
    </div>
    <el-dialog :visible.sync="dialogVisible" width="720px">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="素材库" name="list">
          <el-radio-group @change="changeCollect" v-model="filterParams.collect" size="small">
            <el-radio-button :label="false">全部</el-radio-button>
            <el-radio-button :label="true">收藏</el-radio-button>
          </el-radio-group>
          <div class="img-list">
            <div
              class="img-item"
              v-for="item in images"
              :key="item.id"
              @click="selectImage(item.url)"
              :class="{selected: item.url===selectedImageUrl}"
            >
              <img :src="item.url" alt />
            </div>
          </div>
          <el-pagination
            background
            layout="prev, pager, next"
            @current-change="changePager"
            :current-page="filterParams.page"
            :page-size="filterParams.per_page"
            :total="total"
          ></el-pagination>
        </el-tab-pane>

        <el-tab-pane label="上传图片" name="upload">
          <el-upload
            class="avatar-uploader"
            action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
            name="image"
            :headers="headers"
            :show-file-list="false"
            :on-success="uploadImageSuccess"
          >
            <img v-if="uploadImageUrl" :src="uploadImageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmImage">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import auth from "@/utils/auth.js";
import defaultImage from '@/assets/default.png';
export default {
  props: ['value'],
  name: "my-image",
  data() {
    return {
      dialogVisible: false,
      activeName: "list",
      filterParams: {
        collect: false,
        page: 1,
        per_page: 8
      },
      images: [],
      total: 0,
      selectedImageUrl: null,
      uploadImageUrl: null,
      //confirmImageUrl就是样子货，真正实现双向数据绑定不需要
      //confirmImageUrl: defaultImage,
      headers: {
        Authorization: `Bearer ${auth.getUser().token}`
      },
      defaultImage
    };
  },
  methods: {
    confirmImage() {
      if (this.activeName === "list") {
        if (!this.selectedImageUrl) this.$message.warning("请选择图片");
        //this.confirmImageUrl = this.selectedImageUrl;
        this.$emit('input', this.selectedImageUrl);
        this.dialogVisible = false;
      }

      if (this.activeName === "upload") {
        if (!this.uploadImageUrl) this.$message.warning("请选择图片");
        //this.confirmImageUrl = this.uploadImageUrl;
        this.$emit('input', this.uploadImageUrl);
        this.dialogVisible = false;
      }
    },
    uploadImageSuccess(res) {
      this.$message.success("图片上传成功");
      this.uploadImageUrl = res.data.url;
    },
    selectImage(url) {
      this.selectedImageUrl = url;
    },
    changePager(newpage) {
      this.filterParams.page = newpage;
      this.getImages();
    },
    changeCollect() {
      this.filterParams.page = 1;
      this.getImages();
    },
    openDialog() {
      this.dialogVisible = true;
      this.activeName = "list";
      this.filterParams.page = 1;
      this.getImages();
      this.selectedImageUrl = null;
      this.uploadImageUrl = null;
    },
    async getImages() {
      const {
        data: { data }
      } = await this.$http.get("/user/images", {
        params: this.filterParams
      });
      this.images = data.results;
      this.total = data.total_count;
    }
  }
};
</script>

<style scoped lang='less'>
.my-image {
  display: inline-block;
  margin-top: 10px;
  margin-right: 20px;
}
.img_btn {
  width: 150px;
  height: 150px;
  border: 1px dashed #ddd;
  img {
    width: 100%;
    height: 100%;
    display: block;
  }
}
.img-list {
  margin-bottom: 15px;
  .img-item {
    width: 150px;
    height: 120px;
    display: inline-block;
    margin-top: 15px;
    margin-right: 15px;
    position: relative;
    &.selected {
      &::after {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.3) url(../assets/selected.png) no-repeat
          center / 50px 50px;
      }
    }
    img {
      width: 100%;
      height: 100%;
      border: 1px dashed #ccc;
      vertical-align: bottom;
    }
  }
}
/deep/ .el-dialog__body {
  padding-bottom: 0;
}
</style>